package itesm.coler.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import itesm.coler.data.DAOException;
import itesm.coler.data.DAOFactory;
import itesm.coler.data.DAOUtil;
import itesm.coler.data.WordDAO;
import itesm.coler.models.User;
import itesm.coler.models.Word;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Produces({"application/xml", "application/json"})
@Path("/words")
public class WordResource {
	
	DAOFactory MySQLFactory = DAOFactory.getDAOFactory(DAOFactory.MySQL);
	WordDAO wordDAO = MySQLFactory.getWordDAO();
	
	@GET
	public ArrayList<Word> findAll(){
		return wordDAO.findAll();
	}

	@GET
	 @Path("/problem/{problemId}/")
	 @Produces({ "application/xml", "application/json" })
	 public ArrayList<Word> getWordsByProblem(@PathParam("problemId") Long problemId) {
	    ArrayList<Word> words = null;
	    words = (ArrayList<Word>) wordDAO.findAllByProblem(problemId);
	    return words;
	 }
	
	@PUT
	  @Path("/add")
	  @Produces("text/plain")
	  @Consumes(MediaType.APPLICATION_JSON)
	  public Response addUser(Word word, Long problemId) {
	   try {
		   String results = null;
	       int result = wordDAO.insertWordByProblem(word, problemId);
	       if (result != -1)
	          results = "Word saved : " + word;
	       else
	    	  results = "Word not saved: " + word;
	       return Response.status(201).entity(results).build();
	   
	   } catch (Exception sqle){
		   sqle.printStackTrace();
		   return null;
	   }
	  }
}
